//
//  UtilsMacro.swift
//
//
//  Created by solarke_apple_mini_1 on 2018/3/26.
//  Copyright © 2018年 石家庄科林电气股份有限公司. All rights reserved.
//

import UIKit
import MBProgressHUD
import Toast


// 全局bounds
let windowSize = UIScreen.main.bounds.size

//获取carownerId的值
let userDefault = UserDefaults.standard


//是否为调试模式，注意发布的时候改为true
let jPushProduction = false

//判断网络
let IsReachability = "IsReachability"

let notificationCenter = NotificationCenter.default

//获取设备唯一的值
let uuid = UUID().uuidString

//是否为调试模式，注意发布的时候改为true
let isProduction = true
//报告异常
//let BuglyId = "9adb91c718".

let mainColor = UIColor.colorFromRGB("#359DF5")

let notificationSideStealType = Notification.Name(rawValue: "sideStealType")

//UserDefaults没有获取到值时，返回 defaultValue
func UserDefaultsOrNothingValue(_ key : String,defaultValue:String) -> String {
    let defaults = UserDefaults.standard
    if defaults.string(forKey: key) == nil {
        return defaultValue
    }
    return defaults.string(forKey: key)!
}

func UserDefaultsForBoolValue(_ key : String) -> Bool {
    let defaults = UserDefaults.standard
    return defaults.bool(forKey: key)
}

func UserDefaultsForStringValue(_ key : String) -> String {
    let defaults = UserDefaults.standard
    return defaults.string(forKey: key)!
}

func UserDefaultsForIntValue(_ key : String) -> Int {
    let defaults = UserDefaults.standard
    return defaults.integer(forKey: key)
}

func UserDefaultsForFloatValue(_ key : String) -> Float {
    let defaults = UserDefaults.standard
    return defaults.float(forKey: key)
}

func UserDefaultsForDoubleValue(_ key : String) -> Double {
    let defaults = UserDefaults.standard
    return defaults.double(forKey: key)
}

func UserDefaultsForAnyObjectObject(_ key : String) -> AnyObject {
    let defaults = UserDefaults.standard
    return defaults.object(forKey: key) as AnyObject
}

func SetUserDefaultsBool(_ value : Bool, key : String) {
    let defaults = UserDefaults.standard
    defaults.set(value, forKey: key)
    defaults.synchronize()
}

func SetUserDefaultsString(_ value : String, key : String) {
    let defaults = UserDefaults.standard
    defaults.set(value, forKey: key)
    defaults.synchronize()
}

func SetUserDefaultsInt(_ value : Int, key : String) {
    let defaults = UserDefaults.standard
    defaults.set(value, forKey: key)
    defaults.synchronize()
}

func SetUserDefaultsFloat(_ value : Float, key : String) {
    let defaults = UserDefaults.standard
    defaults.set(value, forKey: key)
    defaults.synchronize()
}

func SetUserDefaultsDouble(_ value : Double, key : String) {
    let defaults = UserDefaults.standard
    defaults.set(value, forKey: key)
    defaults.synchronize()
}

func getJSONString(_ json : AnyObject, key : String, defaultResult : String) -> String {
    var result : String = defaultResult
    if nil != json.object(forKey: key) {
        result = json.object(forKey: key) as! String
    }
    return result
}

func getJSONInt(_ json : AnyObject, key : String, defaultResult : Int) -> Int {
    var result : Int = defaultResult
    if nil != json.object(forKey: key) {
        result = json.object(forKey: key) as! Int
    }
    return result
}

func getJSONDouble(_ json : AnyObject, key : String, defaultResult : Double) -> Double {
    var result : Double = defaultResult
    if nil != json.object(forKey: key) {
        result = json.object(forKey: key) as! Double
    }
    return result
}

//检查长度
func stringIsEmpty(_ inputStr : String) -> Bool {
    // 如果没有值
    if inputStr.isEmpty {
        return true
    }
    
    //检查长度
    let text = inputStr as NSString
    let set = CharacterSet.whitespaces
    let result = text.trimmingCharacters(in: set)
    if (result as NSString).length == 0 {
        return true
    }
    return false
}

func showToast(_ toastContent : String) -> Void {
    let keyWindow = UIApplication.shared.delegate!.window!
    keyWindow?.makeDefaultToast(toastContent)
}

func showLoading(_ show : Bool) -> Void {
    let keyWindow = UIApplication.shared.delegate!.window!
    if show {
        MBProgressHUD.showAdded(to: keyWindow!, animated: true)
    } else {
        MBProgressHUD.hide(for: keyWindow!, animated: true)
    }
}

func getDictionaryFromJSONString(jsonString:String) ->NSDictionary{
    let jsonData:Data = jsonString.data(using: .utf8)!
    let dict = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers)
    if dict != nil {
        return dict as! NSDictionary
    }
    return NSDictionary()
}



//抽屉效果进入的首页
let slideMenuController = UIApplication.shared.keyWindow?.rootViewController as! SlideMenuController

let slideMenu = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "SlideMenu") as! SlideMenuController

let canShuSheZhi = 1
let chaxunKongZhi = 2
let shuJu1Lei = 3
let shuJu2Lei = 4

let WiFiName = "WiFiName"
let WiFiPassword = "WiFiPassword"


let leiShuju = ["日冻结换相开关动作总次数","月冻结换相开关动作总次数"]


let huanXiangControlNotification = Notification.Name(rawValue: "换相控制")
let CommutationSwitchOnlineNotification = Notification.Name(rawValue: "换相开关在线状态")
let VoltageCurrentNotification = Notification.Name(rawValue: "电压不平衡度和电流不平衡度")
let CommutationSwitchDataNotification = Notification.Name(rawValue: "换相开关数据")
let DayFrozenSwitchNumberNotification = Notification.Name(rawValue: "日冻结换相开关动作总次数")
let MonthFrozenSwitchNumberNotification = Notification.Name(rawValue: "月冻结换相开关动作总次数")

let a3:UInt8 = UInt8(12)
func getTermaddr() -> UnsafeMutablePointer<UInt8> {
    let termaddr:UnsafeMutablePointer<UInt8> = UnsafeMutablePointer<UInt8>.allocate(capacity: 4)
    termaddr[0] = UInt8(255)
    termaddr[1] = UInt8(255)
    termaddr[2] = UInt8(255)
    termaddr[3] = UInt8(255)
    return termaddr
}
